// Using_DirectShow_library.cpp: defines the entry point for the console application.
//

#include "stdafx.h"

using namespace DSP;


smartCOM sCOM;
std::auto_ptr<DSplayer<simpleGraph>> player;
bool closeApp = false;


void getFileName(WCHAR* wFileName)
{
	char fileName[MAX_PATH];
	std::cout << "Enter file name: ";
	std::cin >> fileName;
	std::cin.clear();
	MultiByteToWideChar(CP_ACP, 0, fileName, -1, wFileName, MAX_PATH);
}


void eventProcess(long eventCode)
{
	switch(eventCode)
	{
	case EC_COMPLETE:
		player->stop();
		std::cout << "Playback is complete.\n";
		break;
	case EC_USERABORT:
		player->stop();
		break;
	case EC_ERRORABORT:
		player->stop();
		std::cout << "An operation was aborted because of an error.\n";
		break;
	}
}


void errorProcess(const std::exception& ex)
{
	std::cout << ex.what();
	closeApp = true;
}


int _tmain(int argc, _TCHAR* argv[])
{
	LONGLONG pos;
	WCHAR wFileName[MAX_PATH];
	long volume;
	char key;
	Ievent* pEvent;
	std::cout << "f - set file \np - play \nh - pause \ns - stop \nr - seek \n+/- - volume\n\n";
	try
	{
		getFileName(wFileName);
		player.reset(new DSplayer<simpleGraph>(new simpleGraph(wFileName)));

		std::cout << "Enter volume(0...100): ";
		std::cin >> volume;
		player->setVolume(volume);
		player->play();
		do
		{
			key = _getch();
			if(!closeApp)
			{
				switch(key)
				{
				case 'f':
				case'F':
					getFileName(wFileName);
					pEvent->stopEventNotification();
					player.reset(new DSplayer<simpleGraph>(new simpleGraph(wFileName)));
					pEvent = player->getMediaEvent(eventProcess, errorProcess);
					pEvent->startEventNotification();
					player->setVolume(volume);
					player->play();
					break;
				case 'p':
				case 'P':
					player->play();
					break;
				case 'h':
				case 'H':
					player->pause();
					break;
				case 's':
				case 'S':
					player->stop();
					break;
				case 'r':
				case 'R':
					player->getDuration(pos);
					std::cout << "Enter necessary second (duration = " << pos << " second(s)): ";
					std::cin >> pos;
					std::cin.clear();
					player->seek(pos);
					break;
				case '+':
					volume++;
					player->setVolume(volume);
					std::cout << "Volume = " << volume <<'\n';
					break;
				case '-':
					volume--;;
					player->setVolume(volume);
					std::cout << "Volume = " << volume <<'\n';
					break;
				}
			}
			else
				key = 27;
		}
		while(key != 27);
		pEvent->stopEventNotification();
	}
	catch(const errorInfo& erInfo)
	{
		pEvent->stopEventNotification();
		std::cout << erInfo.what();
		_getch();
	}
	catch(const std::exception& ex)
	{
		pEvent->stopEventNotification();
		std::cout << ex.what();
		_getch();
	}
	return 0;
}